/**
 * Copyright (c) 2011-2014, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package com.tq68.weixin.common;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.tq68.weixin.controller.checkin.CheckinController;
import com.tq68.weixin.controller.commchat.CommunityChatController;
import com.tq68.weixin.controller.discounts.DiscountsController;
import com.tq68.weixin.controller.image.ImgController;
import com.tq68.weixin.controller.login.AdminiController;
import com.tq68.weixin.controller.login.LoginController;
import com.tq68.weixin.controller.login.NewuserController;
import com.tq68.weixin.controller.login.SellerLoginController;
import com.tq68.weixin.intercept.PathIntercept;
import com.tq68.weixin.intercept.UserIntercept;
import com.tq68.weixin.model._MappingKit;

public class WeixinConfig extends JFinalConfig {
	
	public static ActiveRecordPlugin arp;
	public static String uploadpath;
	/**
	 * 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
	 * @param pro 生产环境配置文件
	 * @param dev 开发环境配置文件
	 */
	public void loadProp(String pro, String dev) {
		try {
			PropKit.use(pro);
		}
		catch (Exception e) {
			PropKit.use(dev);
		}
	}
	
	public void configConstant(Constants me) {
		loadProp("a_little_config_pro.txt", "a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		// ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
		ApiConfigKit.setDevMode(me.getDevMode());
		uploadpath = PropKit.get("uploadpath");
		if (StringUtils.isNotBlank(uploadpath)) {
			me.setBaseUploadPath(uploadpath);
		}
	}
	
	public void configRoute(Routes me) {
		me.add("/msg", WeixinMsgController.class);
		me.add("/api", WeixinApiController.class, "/api");
		me.add("/login", LoginController.class, "/communityChat");
		me.add("/newuser", NewuserController.class);
		me.add("/img", ImgController.class);
		me.add("/", CommunityChatController.class, "/communityChat");
		me.add("/checkin", CheckinController.class);
		me.add("/communityChat", CommunityChatController.class);
		me.add("/discounts", DiscountsController.class);
		me.add("/sellerlogin", SellerLoginController.class, "/login");
		me.add("/admini", AdminiController.class);
	}
	
	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}
	
	public void configPlugin(Plugins me) {
		 C3p0Plugin c3p0Plugin = createC3p0Plugin();
		 me.add(c3p0Plugin);
		 
		// 配置ActiveRecord插件
		arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setShowSql(true);
		me.add(arp);

		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
		
		//缓存插件
		// EhCachePlugin ecp = new EhCachePlugin();
		// me.add(ecp);
	}
	
	public void configInterceptor(Interceptors me) {
		me.add(new PathIntercept());
		me.add(new UserIntercept());
	}
	
	public void configHandler(Handlers me) {
		
	}
	
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 80, "/tq68_wx", 5);
	}
}
